package cn.kow.kmall.config;

import cn.kow.kmall.common.wxpay.IWXPayDomain;
import cn.kow.kmall.common.wxpay.IWXPayDomainImpl;
import cn.kow.kmall.common.wxpay.WXPayConfig;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


@Component
@EnableAutoConfiguration
public class WXAccountConfig extends WXPayConfig {

    /**
     * app应用ID
     */
    @Value("${weixin.appId}")
    private String appId;

    /**
     * app应用Secret
     */
    @Value("${weixin.appSecret}")
    private String appSecret;

    /**
     * 商户号
     */
    @Value("${weixin.mchId}")
    private String mchId;

    /**
     * 商户密钥
     */
    @Value("${weixin.mchKey}")
    private String mchKey;

    /**
     * 商户证书路径
     */
    @Value("${weixin.certPath}")
    private String certPath;

    /**
     * 微信支付异步通知地址
     */
    @Value("${weixin.notifyUrl}")
    private String notifyUrl;

    public String getAppId() {
        return appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public String getMchId() {
        return mchId;
    }

    public String getMchKey() {
        return mchKey;
    }

    public String getCertPath() {
        return certPath;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    @Override
    public String getAppID() {
        return appId;
    }

    @Override
    public String getMchID() {
        return mchId;
    }

    @Override
    public String getKey() {
        return mchKey;
    }

    @Override
    public InputStream getCertStream() {
//        return ClassLoader.getSystemResourceAsStream(this.certPath);
        byte[] certData = null;
        try {
            InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.certPath);
            certData = IOUtils.toByteArray(certStream);
            certStream.read(certData);
            certStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayInputStream certBis = new ByteArrayInputStream(certData);
        return certBis;
    }

    @Override
    public IWXPayDomain getWXPayDomain() {
        return IWXPayDomainImpl.instance();
    }
}
